﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Web.Mvc;
using RedSand.Web.Mvc.Controls;
using System.Text.RegularExpressions;

namespace RedSand.Site.Controllers
{
    public class ActionController : BaseController
    {
        public ActionResult Manager()
        {
            ViewBag.RoleList = RsDataManager.GetList<RoleInfo>("RoleGetAll");
            return View(ActionList.Items);
        }

        public ActionResult Menu()
        {
            return PartialView(ActionList.Items);
        }

        public ActionResult Save(string[] RoleIDs)
        {
            int count = 0;
            var list = ActionList.Items.Where(item => item.ParentID > 0);
            for(int i=0;i<list.Count();i++)
            {
                if (RsDataManager.Save("PermissionSave", new { Link = list.ElementAt(i).Link, RoleIDs = RoleIDs[i] })<=0)
                {
                    count++;
                }
            }
            return count == 0 ? AjaxMessage(Message.UpdateSuccess) : AjaxMessage(string.Format("Cannot update {0} records", count));
        }

        public ActionResult GetRoleCheckList(string Link)
        {
            return PartialView("RoleCheckList", RsDataManager.GetList<RoleCheckListModel>("RoleGetRoleForLink", new { Link = Link }));
        }
    }
}
